package com.yansu.own.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yansu.own.common.dtos.PageResponseResult;
import com.yansu.own.common.dtos.ResponseResult;
import com.yansu.own.common.enums.AppHttpCodeEnum;
import com.yansu.own.entity.dto.SysMaterialDto;
import com.yansu.own.file.service.FileStorageService;
import com.yansu.own.mapper.SysMaterialMapper;
import com.yansu.own.entity.pojo.SysMaterial;
import com.yansu.own.service.SysMaterialService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Slf4j
@Service
public class SysMaterialServiceImpl extends ServiceImpl<SysMaterialMapper, SysMaterial> implements SysMaterialService {
    @Autowired
    private FileStorageService fileStorageService;

    @Autowired
    private SysMaterialMapper wmMaterialMapper;


    /**
     * 上传素材图片
     */
    @Override
    public ResponseResult<SysMaterial> uploadPicture(MultipartFile multipartFile) {
        //1，如果图片格式不对，返回参数无效
        if(multipartFile == null || multipartFile.getSize() == 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2，上传图片到minio，为图片设置UUID文件名
        //2.1 UUID为文件设置文件名
        String fileName = UUID.randomUUID().toString().replace("-","");
        //2.3 获取文件后缀
        String originalFilename = multipartFile.getOriginalFilename();
        String postfix = originalFilename.substring(originalFilename.lastIndexOf("."));
        String path = null;
        //2.3 上传文件到minio
        try {
            path = fileStorageService.uploadImgFile("/material/picture", fileName+postfix, multipartFile.getInputStream());
            log.info("素材上传成功,");
        } catch (IOException e) {
            e.printStackTrace();
            log.error("WmMaterialServiceImpl-上传文件失败");
        }

        //3，保存数据到数据库
        SysMaterial material = new SysMaterial();
        material.setUserId(1);
        material.setCreateTime(new Date());
        material.setIsCollection((short)0);
        material.setType((short)0);
        material.setUrl(path);
        save(material);

        //4，返回数据
        return ResponseResult.okResult(material);
    }

    /**
     * 查看素材列表
     */
    @Override
    public ResponseResult<List<SysMaterial>> materialList(SysMaterialDto dto) {
        //检验参数
        dto.checkParam();
        //分页查询
        IPage<SysMaterial> page = new Page(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<SysMaterial> wrapper = new LambdaQueryWrapper<>();
        //是否收藏
        wrapper.eq(SysMaterial::getIsCollection,dto.getIsCollection());
        //按照自媒体用户查询
        wrapper.eq(SysMaterial::getUserId,1);
        //按照时间排序
        wrapper.orderByDesc(SysMaterial::getCreateTime);
        page = page(page,wrapper);
        //封装page，size，total
        ResponseResult<List<SysMaterial>> result = new PageResponseResult(dto.getPage(),dto.getSize(),(int)page.getTotal() );
        //封装数据
        result.setData(page.getRecords());
        //结果返回
        return result;
    }
}
